﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QXBook.Model;
using System.Data.Common;
using System.Data.SqlClient;
using QXBook.Utils;
using System.Data;
namespace QXBook.Service
{
    public class ArticleService : ServiceBase
    {
        public void DeleteList(string ids)
        {
            string sql = "DELETE FROM CoreTemplate WHERE CoreID IN (SELECT CoreID FROM Article WHERE ArticleID IN (" + ids + "))";
            sql += "DELETE FROM Article WHERE ArticleID IN (" + ids + "))";
            this.db.ExecuteNonQuery(CommandType.Text, sql);
        }

        public ArticleInfo GetModel(object articleID)
        {
            string sql = "SELECT A.Title,A.PartMod,A.CategoryID,A.Author,A.SmallImg,A.BigImg,A.Content,B.ArticleID,B.CoreID FROM CoreTemplate A JOIN Article B ON B.CoreID=A.CoreID WHERE B.ArticleID=" + articleID.ToString();

            DataSet ds = this.db.ExecuteDataSet(CommandType.Text, sql);

            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                ArticleInfo article = new ArticleInfo();
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    article.ArticleID = CommonUtils.ToInt(dr["ArticleID"]);
                    article.PartMod = (Part)CommonUtils.ToInt(dr["PartMod"]);
                    article.CategoryID = CommonUtils.ToInt(dr["CategoryID"]);
                    article.CoreID = CommonUtils.ToInt(dr["CoreID"]);
                    article.Author = CommonUtils.ToString(dr["Author"]);
                    article.SmallImg = CommonUtils.ToString(dr["SmallImg"]);
                    article.BigImg = CommonUtils.ToString(dr["BigImg"]);
                    article.Title = CommonUtils.ToString(dr["Title"]);
                    article.Content = CommonUtils.ToString(dr["Content"]);
                }

                return article;
            }
            return null;
        }

        public string AddArticle(string AdminName, ArticleInfo article)
        {
            MembersInfo admininfo = ServiceFactory.CreateInstance<AdminService>().GetAdminEntityByAdminName(AdminName);
            //判断当前管理员是否存在
            string rtmessage = ServiceFactory.CreateInstance<AdminService>().CheckAdminPower(admininfo);
            if (!string.IsNullOrEmpty(rtmessage)) return rtmessage;

            DbCommand cmd = new SqlCommand();
            cmd.CommandText = @"INSERT INTO CoreTemplate(Title,PartMod,CategoryID,Content,Author,SmallImg,BigImg,IsPublish,AddDate) 
                                VALUES (@Title,@PartMod,@CategoryID,@Content,@Author,@SmallImg,@BigImg,@IsPublish,GETDATE());SELECT @@IDENTITY";//返回主键

            base.db.AddInParameter(cmd, "@Title", DbType.String, article.Title);
            base.db.AddInParameter(cmd, "@PartMod", DbType.Int16, (int)article.PartMod);
            base.db.AddInParameter(cmd, "@CategoryID", DbType.String, article.CategoryID);
            base.db.AddInParameter(cmd, "@Content", DbType.String, article.Content);
            base.db.AddInParameter(cmd, "@Author", DbType.String, article.Author);
            base.db.AddInParameter(cmd, "@SmallImg", DbType.String, article.SmallImg);
            base.db.AddInParameter(cmd, "@BigImg", DbType.String, article.BigImg);
            base.db.AddInParameter(cmd, "@IsPublish", DbType.Int16, (int)article.IsPublish);

            int identity = 0;
            object obj = base.db.ExecuteScalar(cmd);
            if (obj != null)
            {
                identity = CommonUtils.ToInt(obj);
            }

            cmd.CommandText = @"INSERT INTO Article(CoreID,ShortContent,KeyWord) 
                                VALUES (@CoreID,@ShortContent,@KeyWord)";

            base.db.AddInParameter(cmd, "@CoreID", DbType.Int32, identity);
            base.db.AddInParameter(cmd, "@ShortContent", DbType.String, article.ShortContent);
            base.db.AddInParameter(cmd, "@KeyWord", DbType.String, article.KeyWord);

            base.db.ExecuteNonQuery(cmd);
            return string.Empty;
        }

        public string UpdateArticle(string AdminName, ArticleInfo article)
        {
            MembersInfo admininfo = ServiceFactory.CreateInstance<AdminService>().GetAdminEntityByAdminName(AdminName);
            //判断当前管理员是否存在
            string rtmessage = ServiceFactory.CreateInstance<AdminService>().CheckAdminPower(admininfo);
            if (!string.IsNullOrEmpty(rtmessage)) return rtmessage;

            string SQL = "UPDATE CoreTemplate SET ";
            SQL += "Title=@Title,PartMod=@PartMod,CategoryID=@CategoryID,Content=@Content,Author=@Author,SmallImg=@SmallImg,BigImg=@BigImg,IsPublish=@IsPublish,UpdateDate=GETDATE() WHERE CoreID=@CoreID";

            DbCommand cmd = new SqlCommand(SQL);
            base.db.AddInParameter(cmd, "@Title", DbType.String, article.Title);
            base.db.AddInParameter(cmd, "@PartMod", DbType.Int16, (int)article.PartMod);
            base.db.AddInParameter(cmd, "@CategoryID", DbType.Int32, article.CategoryID);
            base.db.AddInParameter(cmd, "@Content", DbType.String, article.Content);
            base.db.AddInParameter(cmd, "@Author", DbType.String, article.Author);
            base.db.AddInParameter(cmd, "@SmallImg", DbType.String, article.SmallImg);
            base.db.AddInParameter(cmd, "@BigImg", DbType.String, article.BigImg);
            base.db.AddInParameter(cmd, "@IsPublish", DbType.Int16, (int)article.IsPublish);
            base.db.AddInParameter(cmd, "@CoreID", DbType.Int32, article.CoreID);
            base.db.ExecuteNonQuery(cmd);

            cmd.CommandText = @"UPDATE Article SET 
                    ShortContent=@ShortContent,KeyWord=@KeyWord WHERE ArticleD=@ArticleID";
            base.db.AddInParameter(cmd, "@ShortContent", DbType.String, article.ShortContent);
            base.db.AddInParameter(cmd, "@KeyWord", DbType.String, article.KeyWord);
            base.db.AddInParameter(cmd, "@ArticleID", DbType.Int32, article.ArticleID);

            return string.Empty;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="nTotal"></param>
        /// <returns></returns>
        public List<ArticleInfo> GetTopList(int nTotal)
        {
            List<ArticleInfo> list = new List<ArticleInfo>();
            DbCommand cmd = new SqlCommand();

            StringBuilder sbSql = new StringBuilder();
            sbSql.AppendFormat("SELECT TOP {0} A.ArticleID,B.Title,B.SmallImg,B.IsPublish,B.AddDate,B.UpdateDate FROM CoreTemplate B JOIN Article A ON B.CoreID=A.CoreID", nTotal);
            sbSql.Append(" LEFT JOIN Category C ON B.CategoryID=C.CategoryID ");
            sbSql.Append(" Order by A.ArticleID DESC");

            cmd.CommandText = sbSql.ToString();
            DataSet ds = this.db.ExecuteDataSet(cmd);

            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    ArticleInfo art = new ArticleInfo();
                    art.ArticleID = CommonUtils.ToInt(dr["ArticleID"]);
                    art.Title = CommonUtils.ToString(dr["Title"]);
                    art.SmallImg = CommonUtils.ToString(dr["SmallImg"]);
                    art.IsPublish = (YesNo)CommonUtils.ToInt(dr["IsPublish"]);
                    art.AddDate = CommonUtils.ToTime(dr["AddDate"].ToString());
                    art.UpdateDate = CommonUtils.ToTime(dr["UpdateDate"].ToString());
                    list.Add(art);
                }
            }

            return list;
        }

        /// <summary>
        /// 获取资讯列表
        /// </summary>
        /// <param name="ps"></param>
        /// <param name="condition"></param>
        /// <returns></returns>
        public List<ArticleInfo> GetModelList(ref PagerState ps, ArticleCondition condition)
        {
            List<ArticleInfo> list = new List<ArticleInfo>();
            DbCommand cmd = new SqlCommand();

            StringBuilder sbSql = new StringBuilder();
            sbSql.Append("SELECT A.ArticleID,B.Title,B.SmallImg,B.IsPublish,B.AddDate,B.UpdateDate,C.CategoryID,C.CategoryName FROM CoreTemplate B JOIN Article A ON B.CoreID=A.CoreID");
            sbSql.Append(" LEFT JOIN Category C ON B.CategoryID=C.CategoryID ");
            sbSql.Append(" WHERE 1=1");
            if (condition != null)
            {
                if (condition.IsPublish != YesNo.Unknow)
                {
                    sbSql.Append(" AND B.IsPublish=" + (int)condition.IsPublish);
                }
            }

            DataTable dt = this.GetListPager(ref ps, sbSql.ToString(), "ArticleID", cmd);

            if (dt != null && dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    ArticleInfo art = new ArticleInfo();
                    art.ArticleID = CommonUtils.ToInt(dr["ArticleID"]);
                    art.CategoryID = CommonUtils.ToInt(dr["CategoryID"]);
                    art.Title = CommonUtils.ToString(dr["Title"]);
                    art.SmallImg = CommonUtils.ToString(dr["SmallImg"]);
                    art.IsPublish = (YesNo)CommonUtils.ToInt(dr["IsPublish"]);
                    art.AddDate = CommonUtils.ToTime(dr["AddDate"].ToString());
                    art.UpdateDate = CommonUtils.ToTime(CommonUtils.ToString(dr["UpdateDate"]));
                    
                    art.Categorie = new CategoryInfo();
                    art.Categorie.CategoryID = CommonUtils.ToInt(dr["CategoryID"]);
                    art.Categorie.CategoryName = CommonUtils.ToString(dr["CategoryName"]);
                    list.Add(art);
                }
            }

            return list;
        }
    }
}
